﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using RubricaSentimentale.Model;
using Microsoft.Phone.Shell;

namespace RubricaSentimentale
{
    public partial class MainPage : PhoneApplicationPage
    {
        ViewModel vm;
        bool firsttime=true;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            //Contatti.DataContext = vm.agenda.DisplayList;  

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (firsttime)
            {
                vm = (ViewModel)Utility.Load(typeof(ViewModel));
                this.DataContext = vm.agenda;
                vm.agenda.DisplayList = vm.agenda.Persone;
                Utility.SaveState("ViewModel", vm);
                firsttime = false;
            }
        }

        private void Element_Tap(object sender, GestureEventArgs e)
        {
            vm.Current = (Persona)(sender as Grid).DataContext;
            Utility.SaveState("ViewModel",vm);

            NavigationService.Navigate(new Uri("/View/Visualizza.xaml",UriKind.Relative));
        }

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            Utility.SaveState("ViewModel", vm);
            NavigationService.Navigate(new Uri("/View/AggiungiPersona.xaml", UriKind.Relative));
        }

        private void tbxSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            vm.agenda.Search(tbxSearch.Text);
        }
        
    }
}